package com.csw.android.dev_utils.result

import android.app.Activity
import android.content.Intent
import android.os.Bundle

/**
 * 跳转界面获取返回结果的任务对象
 */
open class ActivityRequestTask(
    val requestIntent: Intent,
    val requestData: Bundle?,
    private val callback: Callback
) {
    /**
     * 响应结果码，正常返回[Activity.RESULT_OK]，用户取消[Activity.RESULT_CANCELED]，失败null
     */
    var resultCode: Int? = null
    var data: Intent? = null

    init {
        requestData?.run {
            requestIntent.putExtras(this)
        }
    }

    fun onActivityResult(resultCode: Int?, data: Intent?) {
        this.resultCode = resultCode
        this.data = data
        callback.onTaskResult(this)
    }

    fun isResultOk(): Boolean {
        return resultCode == Activity.RESULT_OK
    }

    fun isResultCancel(): Boolean {
        return resultCode == Activity.RESULT_CANCELED
    }

    fun isResultFailed(): Boolean {
        return resultCode == null
    }
}